package com.kmair.caiwu.controller.attendance;

import com.kmair.caiwu.attendance.pojo.VacationStatus;
import com.kmair.caiwu.attendance.pojo.VacationUsedRecord;
import com.kmair.caiwu.attendance.services.VacationStatusService;
import com.kmair.caiwu.attendance.services.VacationUserRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Calendar;
import java.util.List;

@Controller
@RequestMapping("/attendance")
public class AttendanceController {

    private final VacationStatusService vssDao;
    private final VacationUserRecordService vusrDao;

    @Autowired
    public AttendanceController(VacationStatusService vssDao, VacationUserRecordService vusrDao) {
        this.vssDao = vssDao;
        this.vusrDao = vusrDao;
    }

    @RequestMapping(method = RequestMethod.GET)
    public String showAttendances(Model model) {
        List<VacationStatus> vacationStatusList = vssDao.selectSelective(new VacationStatus());
        model.addAttribute("vacationStatusList", vacationStatusList);
        return "attendance/index";
    }

    @RequestMapping(value = "/{uId}", method = RequestMethod.GET, produces = "application/json")
    public
    @ResponseBody
    List<VacationUsedRecord> getAttendances(@PathVariable String uId) {
        return vusrDao.selectSelectiveByGroup(uId, Calendar.getInstance().get(Calendar.YEAR));
    }

    @RequestMapping(value = "/{uId}", method = RequestMethod.GET, produces = "text/html")
    public String showAttendances1(@PathVariable String uId, Model model) {
        model.addAttribute(vusrDao.selectSelectiveByGroup(uId, Calendar.getInstance().get(Calendar.YEAR)));
        return "attendance/jiban_record";
    }

}
